package com.taptech.doufu.ui.viewholder;

import android.content.Context;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.util.StringUtil;
import com.taptech.doufu.bean.cp.CpBean;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.ui.view.drawcircle.BaseRecyclerViewViewHolder;
import com.taptech.doufu.ui.view.personalcenter.TagsCollectListView;
import com.taptech.doufu.services.CpService;
import com.taptech.doufu.util.ImageManager;
import com.taptech.doufu.util.TagsUtil;

/**
 * Created by lsy on 2015/8/19.
 */
public class TagCollectViewHolder<T extends CpBean> extends BaseRecyclerViewViewHolder implements View.OnClickListener, View.OnLongClickListener
{
   public static int HOME_SEARCH_TYPE = 3;
    private int type;
    private Context mContext;

    // UI
    private RelativeLayout mTagCollectItemBackground;
    private TextView mTagName, mCollectedNum, mContentNum, tag_frist_tv;
    private Button removeCollectBtn;
    public ImageView tag_iv;
    // 数据
    private T contentBean;
    private int mPosition;


    public TagCollectViewHolder(Context context, View itemView)
    {
        super(context, itemView);
        this.mContext = context;
        mTagCollectItemBackground = (RelativeLayout) itemView.findViewById(R.id.tag_collect_item_background);

        mTagName = (TextView) itemView.findViewById(R.id.tag_name_text);
        mCollectedNum = (TextView) itemView.findViewById(R.id.collect_num_text);
        mContentNum = (TextView) itemView.findViewById(R.id.content_num_text);
        removeCollectBtn = (Button) itemView.findViewById(R.id.remove_collect_btn);
        tag_iv = (ImageView) itemView.findViewById(R.id.tag_iv);
        tag_frist_tv = (TextView) itemView.findViewById(R.id.tag_frist_tv);
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public void setViewHolderChildViewContent(MineAbstractBean bean, int position)
    {
        if (bean == null)
        {
            return;
        }
        contentBean = (T) bean;
        if (contentBean == null)
        {
            return;
        }
        mPosition = position;
        // 点击进入详情
        mTagCollectItemBackground.setOnClickListener(this);
        mTagCollectItemBackground.setOnLongClickListener(this);
        // 点击取消收藏
        removeCollectBtn.setOnClickListener(this);
        mTagName.setText(contentBean.getName());
        mCollectedNum.setText(contentBean.getFollowing_count());
        mContentNum.setText(contentBean.getCount());
        int type_id = 0;
        if (contentBean.getType_id() != null)
        {
            type_id = Integer.parseInt(contentBean.getType_id());
            switch (type_id)
            {
                case 18:
                    tag_iv.setImageResource(R.drawable.cp_deaf_iv);
                    break;
                default:
                    tag_iv.setImageResource(R.drawable.cp_defult_iv_bac);
                    break;
            }
        }
        if (contentBean.getIcon() != null && !contentBean.getIcon().equals(""))
        {
            ImageManager.displayImage(tag_iv, contentBean.getIcon(), 0);
            tag_frist_tv.setVisibility(View.GONE);
        } else
        {
            switch (type_id)
            {
                case 18:
                    //tag_iv.setImageResource(R.drawable.cp_deaf_iv);
                    break;
                default:
                    // tag_iv.setImageResource(R.drawable.cp_deaf_iv);
                    tag_frist_tv.setVisibility(View.VISIBLE);
                    tag_frist_tv.setText(contentBean.getName().substring(0, 1));
                    break;

            }
        }
        if(type != HOME_SEARCH_TYPE){
            if (contentBean.getFollowing().equals("1"))
            {
                removeCollectBtn.setText("取消");
            } else
            {
                removeCollectBtn.setText("订阅");
            }
        }else {
            removeCollectBtn.setVisibility(View.GONE);
        }


    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.tag_collect_item_background:
                // TagsUtil.enterTagSearchActivity(mContext , contentBean.getTagName());
                if(!StringUtil.stringLength(contentBean.getType_id())){
//                    TagsUtil.enterTagDesActivity(mContext, contentBean.getId());
                    TagsUtil.openWeexTagActivity(mContext,contentBean.getName(), Constant.OBJECT_TYPE_NOVEL,"");
                    return;
                }
                switch (Integer.parseInt(contentBean.getType_id()))
                {
                    case 18:
                        CpService.enterCpDesActivity(mContext, contentBean.getId());
                        break;
                    default:
                        TagsUtil.openWeexTagActivity(mContext,contentBean.getName(), Constant.OBJECT_TYPE_NOVEL,"");
                        break;
                }
                break;
            case R.id.remove_collect_btn:
                Message msg = Message.obtain();
                if (removeCollectBtn.getText().toString().equals("取消"))
                {
                    msg.what = TagsCollectListView.REMOVE_TAG_COLLECT_CANCEL;
                } else if (removeCollectBtn.getText().toString().equals("订阅"))
                {
                    msg.what = TagsCollectListView.REMOVE_TAG_COLLECT_ADD;
                }
                msg.obj = contentBean;
                msg.arg1 = mPosition;
                //msg.arg2 = Integer.parseInt(contentBean.getType_id());*/
                mHandler.sendMessage(msg);
                break;
        }

    }

    @Override
    public boolean onLongClick(View view)
    {
      /*  Message msg = Message.obtain();
        msg.what = TagsCollectListView.REMOVE_TAG_COLLECT;
        msg.arg1 = mPosition;
        msg.obj = contentBean.getTagName();
        mHandler.sendMessage(msg);*/
        return false;
    }
}
